import { Body, Controller, Delete, Get, Param, ParseArrayPipe, ParseIntPipe, Post, Put, Query, Req, Res } from '@nestjs/common';
import { ParseIntArrayPipe } from '@/common/pipe/parse-int-array.pipe';
import Result from '@/common/result/Result';
import { ApiBody, ApiOperation, ApiQuery, ApiResponse, ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { RequirePermission } from '@/common/decorator/require-premission.decorator';
import { nowDateTime } from '@/common/utils';
import { FrontAccountService } from './service/front-account.service';
import { QueryFrontAccountDto, CreateFrontAccountDto, UpdateFrontAccountDto } from './dto/index';
import { Response } from 'express';
import { FrontAccount } from '@prismaClient';
import { TableDataInfo } from '@/common/domain/TableDataInfo';
@ApiTags("front_account")
@ApiBearerAuth()
@Controller('system/account')
export class FrontAccountController {
    constructor(private accountService: FrontAccountService) { } 
    @ApiOperation({ summary: "查询front_account列表" })
    @ApiResponse({ type: TableDataInfo<FrontAccount> })
    @RequirePermission("system:account:list")
    @Get("/list")
    async listAccount(@Query() q: QueryFrontAccountDto): Promise<TableDataInfo<FrontAccount>> {
       return Result.TableData(await this.accountService.selectAccountList(q))
    }
    @ApiOperation({ summary: "查询front_account所有" })
    @ApiResponse({ type: Result<FrontAccount[]> })
    @RequirePermission("system:account:list")
    @Get("/data")
    async listAccountData(): Promise<Result<FrontAccount[]>> {
       return Result.ok(await this.accountService.selectAccountAll())
    }
    @ApiOperation({ summary: "导出front_accountxlsx文件" })
    @RequirePermission("system:account:export")
    @Get("/export")
    async export(@Res() res: Response): Promise<void> {
       return this.accountService.exportAccount(res)
    }
    @ApiOperation({ summary: "查询front_account详细" })
    @ApiResponse({ type: Result<FrontAccount> })
    @RequirePermission("system:account:list")
    @Get("/:id")
    async getAccount(@Param("id", ParseIntPipe) id: number): Promise<Result<FrontAccount>> {
        return Result.ok(await this.accountService.selectAccountById(id))
    }
    @ApiOperation({ summary: "新增front_account" })
    @ApiResponse({ type: Result<FrontAccount> })
    @ApiBody({ type: CreateFrontAccountDto })
    @RequirePermission("system:account:add")
    @Post("/")
    async addAccount(@Body() frontAccount: CreateFrontAccountDto, @Req() req): Promise<Result<FrontAccount>> {
                                return Result.ok(await this.accountService.addAccount(frontAccount))
    }
   @ApiOperation({ summary: "修改front_account" })
   @ApiResponse({ type: Result<any> })
   @ApiBody({ type: UpdateFrontAccountDto })
   @RequirePermission("system:account:edit")
   @Put("/")
    async updateAccount(@Body() frontAccount: UpdateFrontAccountDto, @Req() req): Promise<Result<any>> {
        await this.accountService. updateAccount(frontAccount)
        return Result.ok("修改成功！")
    }
    @ApiOperation({ summary: "删除front_account" })
    @ApiResponse({ type: Result<any> })
    @RequirePermission("system:account:remove")
    @Delete("/:ids")
    async delAccount(@Param("ids", ParseIntArrayPipe) ids: number[]): Promise<Result<any>> {
        let { count } = await this.accountService.deleteAccountByIds(ids)
        return Result.toAjax(count)
    }
}
